/*
 * Copyright 2012-2016 the original author or authors. Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.amado.springboot.springbootcms.controll;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amado.springboot.springbootcms.domain.User;
import com.amado.springboot.springbootcms.service.UserService;
import com.amado.springboot.springbootcms.vo.UserLoginVO;

@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger (UserController.class);
    public static final String USER_SESSION = "USER_SESSION";
    @Autowired
    private UserService userService;

    @GetMapping
    public String index() {
        LOGGER.debug ("进来userList");
        return "/user/userList";
    }

    @PostMapping("/add")
    @ResponseBody
    public User add(User user) {
        LOGGER.debug ("进来" + user);
        userService.addUser (user);

        LOGGER.debug ("返回来" + userService.getAllUser ());
        return user;
    }

    @GetMapping("/toModify")
    public String get(@RequestParam("id") Long id, HttpServletRequest request) {
        request.setAttribute ("user", userService.get (id));
        return "user/userModify";
    }


    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @GetMapping("/getUser")
    public String getUser(@RequestParam("id") Long id, HttpServletRequest request) {

        request.setAttribute ("user", userService.get (id));
        return "user/getUser";
    }


    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")})
    @PostMapping("/modify")
    @ResponseBody
    public void modify(User user) {
             LOGGER.debug ("6地址{}" + user);
        userService.modify (user);

    }


    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @DeleteMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("ids") String ids) {
        LOGGER.debug ("zhegeshi{}", ids);
        String[] user = ids.split (",");
        Long[] str2 = new Long[user.length];
        for (int i = 0; i < user.length; i++) {
            str2[i] = Long.valueOf (user[i]);
        }
        userService.delete (Arrays.asList (str2));
        return "1";
    }

    @ApiOperation(value = "获取用户列表", notes = "")
    @GetMapping("/list")
    @ResponseBody


    public List<User> list(Map<String, Object> model) {
        LOGGER.debug ("进来" + userService.getAllUser ());
        return userService.getAllUser ();
    }

    @PostMapping("/login")
    public String login(UserLoginVO userLoginVO, HttpServletRequest request) {
        User user = userService.loginAndGet (userLoginVO);
        // 登陆成功
        if (user != null) {
            LOGGER.info ("{} 登陆成功", userLoginVO.getUserAccount ());
            request.getSession ().setAttribute (USER_SESSION, user);
            return "index";
        }
        LOGGER.info ("{} 登陆失败", userLoginVO.getUserAccount ());
        return "login";
    }

    @GetMapping("/loginOut")
    public String loginOut(UserLoginVO userLoginVO, HttpServletRequest request) {
        request.getSession ().removeAttribute (USER_SESSION);
        return "login";
    }
}
